import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import '../xapp_utils.dart';

class ToastUtils {
  static ToastUtils? _instance;

  static ToastUtils getInstance() {
    if (_instance == null) {
      _instance = new ToastUtils._();
    }
    return _instance!;
  }

  _Toast? _toast;

  ToastUtils._();

  void show(String msg,
      [Duration duration = const Duration(milliseconds: 1500)]) async {
    _toast?.close();
    _toast = null;
    _Toast toast = _Toast(msg);
    _toast = toast;
    toast.show();
    await Future.delayed(duration);
    toast.close();
  }
}

class _Toast {
  String msg;
  OverlayEntry? overlayEntry;
  _Toast(this.msg);
  void show() {
    var overlayState = Overlay.of(XAppUtils.getInstance().getContext());
    overlayEntry = new OverlayEntry(builder: (context) {
      return _ToastWidget(msg);
    });
    overlayState?.insert(overlayEntry!);
  }

  void close() {
    overlayEntry?.remove();
    overlayEntry = null;
  }
}

class _ToastWidget extends StatefulWidget {
  final String msg;
  _ToastWidget(this.msg);
  @override
  State<StatefulWidget> createState() => _ToastWidgetState();
}

class _ToastWidgetState extends State<_ToastWidget>
    with TickerProviderStateMixin {
  AnimationController? animationController;
  Animation<double>? animation;
  @override
  void initState() {
    super.initState();
    animationController =
        AnimationController(duration: Duration(milliseconds: 400), vsync: this);
    animation = TweenSequence([
      TweenSequenceItem(
          tween: Tween(begin: 0.2, end: 1.0)
              .chain(CurveTween(curve: Curves.easeIn)),
          weight: 10),
      TweenSequenceItem(
          tween: Tween(begin: 1.0, end: 0.9)
              .chain(CurveTween(curve: Curves.easeIn)),
          weight: 20),
      TweenSequenceItem(
          tween: Tween(begin: 0.9, end: 1.0)
              .chain(CurveTween(curve: Curves.easeIn)),
          weight: 70),
    ]).animate(animationController!);
    //开始动画
    animationController?.forward();
  }

  @override
  void dispose() {
    animationController?.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Theme(
      data: Theme.of(context),
      child: Container(
        alignment: Alignment.bottomCenter,
        padding: EdgeInsets.only(
            bottom: XAppUtils.getInstance().getMediaHeight() / 3),
        child: Text.rich(TextSpan(children: [
          WidgetSpan(
              style: TextStyle(
                color: Colors.white,
                fontSize: 13.rpx,
                fontWeight: FontWeight.normal,
                decorationColor: Colors.transparent,
                decoration: TextDecoration.none,
              ),
              child: AnimatedBuilder(
                animation: animation!,
                builder: (c, child) {
                  return Transform.scale(
                    alignment: Alignment.center,
                    scale: animation!.value,
                    child: child,
                  );
                },
                child: Container(
                    transformAlignment: Alignment.bottomCenter,
                    constraints: BoxConstraints(
                        maxWidth: XAppUtils.getInstance().getMediaWidth() * 0.8),
                    decoration: BoxDecoration(
                        color: rgba(0, 0, 0, 0.7),
                        borderRadius: BorderRadius.circular(4.rpx)),
                    padding: EdgeInsets.only(
                        left: 15.rpx,
                        right: 15.rpx,
                        top: 10.rpx,
                        bottom: 10.rpx),
                    child: Text(
                      widget.msg,
                      style: TextStyle(
                        height: 1.2,
                        color: Colors.white,
                        fontSize: 13.rpx,
                        fontWeight: FontWeight.normal,
                        decorationColor: Colors.transparent,
                        decoration: TextDecoration.none,
                      ),
                    )),
              ))
        ])),
      ),
    );
  }
}
